package com.stud.mc.controller;

import com.stud.common.core.domain.Result;
import com.stud.common.jdbc.MyJdbcTemplate;
import com.stud.mc.convert.Convertor;
import com.stud.mc.domain.Repository;
import com.stud.mc.dto.RepositoryDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3FileReader;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.TagException;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RestController
@Slf4j
@Tag(name = "音乐模块测试")
@RequestMapping("/mcTest")
public class McTestController {

    private final MyJdbcTemplate jdbcTemplate;

    @PostMapping("/test1")
    @Operation(summary = "测试1")
    public Result<RepositoryDTO> test1(@ParameterObject Repository repository) {
        repository.setName("张三");
        repository.setBits(6658L);
        return Result.ok(Convertor.INSTANCE.convert(repository));
    }

    @PostMapping("/test2")
    @Operation(summary = "测试2")
    public Result<?> test2() {
        try {
            File file = new File("");
            MP3AudioHeader audioHeader = new MP3AudioHeader(file);
            MP3FileReader reader = new MP3FileReader();
            AudioFile audioFile = reader.read(file);

            System.out.println("标题:" + audioFile.getTag().getFirst(FieldKey.TITLE));
            System.out.println("作者:" + audioFile.getTag().getFirst(FieldKey.ARTIST));
            System.out.println("专辑:" + audioFile.getTag().getFirst(FieldKey.ALBUM));
            System.out.println("比特率:" + audioHeader.getBitRate());
            System.out.println("时长:" + audioHeader.getTrackLengthAsString() + " (" + audioHeader.getTrackLength() + "s)");
            System.out.println("大小:" + (file.length() / 1024F / 1024F) + "MB");
            System.out.println(" ----- ----- ");


            return Result.ok();
        } catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/jdbcTest")
    @Operation(summary = "jdbcTest")
    public Result<?> jdbcTest() {
        List<Map<String, Object>> list = jdbcTemplate.select2Map("select * from sys_user");
        return Result.ok(list);
    }

}
